﻿using MahApps.Metro.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace LunarSF.SHomeWorkshop.LunarMarkdownEditor
{
    /// <summary>
    /// GotoWindow.xaml 的交互逻辑
    /// </summary>
    public partial class GotoWindow : MetroWindow
    {
        /// <summary>
        /// 创建一个“Goto”窗口。用于在各文件、链接、标题、任务间跳转。
        /// </summary>
        public GotoWindow()
        {
            InitializeComponent();

            treeView.Tag = this;
            this.Loaded += GotoWindow_Loaded;

            //已无必要，改用 MetroTreeViewItem 样式后，即使失去焦点，也会保留选中状态。
            //treeView.SelectedItemChanged += TvFind_SelectedItemChanged;
        }

        //private void TvFind_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        //{
        //    //已无必要，改用 MetroTreeViewItem 样式后，即使失去焦点，也会保留选中状态。
        //    //IFindItem oldItem = (e.OldValue as IFindItem);
        //    //if (oldItem != null)
        //    //{
        //    //    oldItem.HeaderTextBlock.Foreground = oldItem.ForegroundOfText;
        //    //    oldItem.BorderBrush = Brushes.Transparent;
        //    //}

        //    //var newItem = (e.NewValue as IFindItem);
        //    //if (newItem != null)
        //    //{
        //    //    newItem.HeaderTextBlock.Foreground = Brushes.Black;
        //    //    newItem.BorderBrush = Brushes.Blue;
        //    //}
        //}

        /// <summary>
        /// 初始化一些信息。
        /// </summary>
        private void GotoWindow_Loaded(object sender, RoutedEventArgs e)
        {
            if (treeView.Items.Count <= 0) return;

            var fstDocumentItem = treeView.Items[0] as FindDocumentTreeViewItem;
            if (fstDocumentItem == null || fstDocumentItem.Items.Count <= 0) return;
            
            var fstFindLineItem = fstDocumentItem.Items[0] as FindLineTreeViewItem;
            if (fstFindLineItem == null) return;

            fstFindLineItem.IsSelected = true;

            this.InvalidateVisual();

            fstFindLineItem.Focus();
        }
                
        /// <summary>
        /// 跳转到指定位置。
        /// </summary>
        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            var selItem = treeView.SelectedItem as FindLineTreeViewItem;
            if (selItem == null)
            {
                this.DialogResult = true;
                this.Close();
                return;
            }

            selItem.GotoLine();
            this.DialogResult = true;
            return;
        }

        /// <summary>
        /// 放弃跳转，关闭窗口。
        /// </summary>
        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }

        /// <summary>
        /// 回车键跳转、Esc键放弃。
        /// </summary>
        private void MetroWindow_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                if (treeView.SelectedItem == null) return;

                var findLineTreeViewItem = treeView.SelectedItem as FindLineTreeViewItem;
                if (findLineTreeViewItem == null) return;

                findLineTreeViewItem.GotoLine();
                this.DialogResult = true;
                this.Close();
                return;
            }
            else if (e.Key == Key.Escape)
            {
                this.DialogResult = false;
                this.Close();
            }
        }
    }
}
